package ru.acme.lab11.domain.account;

import ru.acme.lab11.exceptions.NotEnoughFundsException;
import ru.acme.lab11.exceptions.OverdraftLimitExceedException;

public class CheckingAccount extends AbstractAccount {

	private int overdraft;
	
	public int getMaxWithdraw() {
		return getBalance() + overdraft;
	}
	
	public CheckingAccount(int balance, int overdraft) {
		super(balance);
		if (overdraft < 0) {
			throw new IllegalArgumentException();
		}
		this.overdraft = overdraft;
	}

	public void withdraw(int amount) throws NotEnoughFundsException {
		if (super.getBalance() + overdraft < amount) {
			throw new OverdraftLimitExceedException(amount - super.getBalance() - overdraft, this);
		}
		if (super.getBalance() >= amount) {
			super.withdraw(amount);
		} else {
			amount -= super.getBalance();
			super.withdraw(super.getBalance());
			overdraft -= amount;
		}
	}

	public int maximumAmountToWihdraw() {
		return overdraft + super.maximumAmountToWihdraw();
	}

}
